const router = require('koa-router')()
const mysql = require('mysql');
const { query } = require('../public/javascripts/db')

router.prefix('/editor')

router.post('/getdata', async (ctx, next) => {
    let option = { 'ok': false };
    let { userId } = ctx.request.body;
    let sql = 'select * from target where user_id=?';
    await query(sql, [userId]).then((results) => { option.ok = true; option.goalList = results });
    ctx.body = option;
})

router.post('/submit', async (ctx, next) => {
    let options = { 'ok': false };
    let { MorD, ym, content, day, userId } = ctx.request.body;
    if (MorD == 'month') {
        let sql = 'insert into review(ym,month_content,user_id) value(?,?,?)';
        await query(sql, [ym, content, userId]).then(options.ok = true);
    } else {
        let sql = 'insert into reviewdc(ym,content,days,user_id) value(?,?,?,?)';
        await query(sql, [ym, content, day, userId]).then(options.ok = true);
    }
    ctx.body = options;
})

module.exports = router